package com.shop.shop_end.service.impl;

import com.shop.shop_end.dto.TopicVo;
import com.shop.shop_end.entity.Topic;
import com.shop.shop_end.exception.ServiceException;
import com.shop.shop_end.mapper.TopicMapper;
import com.shop.shop_end.service.TopicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.shop_end.utils.DateUtils;
import com.shop.shop_end.utils.result.Result;
import com.shop.shop_end.utils.result.ResultUtil;
import com.shop.shop_end.variable.EnableVariable;
import com.shop.shop_end.variable.ErrorCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2024-04-23
 */
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {
    final Logger logger = LoggerFactory.getLogger(TopicServiceImpl.class);

    @Resource
    private TopicMapper topicMapper;

    @Override // 继承接口的新增 返回值0(失败),1(成功)
    @Transactional
    public Result insertTopic(Topic topic) {
        try {
            topic.setCreateTime(new Date());
            int insert = this.topicMapper.insert(topic);
            if (insert == EnableVariable.UNABLE){
                logger.info("增加失败");
                throw new ServiceException(ErrorCodeEnum.SQL_INSERT_ERROR, "增加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.info("增加出现异常");
            throw new ServiceException(ErrorCodeEnum.SQL_INSERT_ERROR, "增加出现异常");
        }
        logger.info("增加成功");
        return ResultUtil.ok("增加成功", topic);
    }

    @Override // 继承接口的更新 返回值0(失败),1(成功)
    public Result updateTopic(Topic topic) {
        logger.info("topic==>{}", topic);
        try {
            logger.info("开始修改topicId为【{}】的topic", topic.getTopicId());
            int update = this.topicMapper.updateById(topic);
            if (update == EnableVariable.UNABLE){
                logger.info("修改失败");
                throw new ServiceException(ErrorCodeEnum.SQL_UPDATE_ERROR, "修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.info("修改出现异常");
            throw new ServiceException(ErrorCodeEnum.SQL_UPDATE_ERROR, "修改出现异常");
        }
        logger.info("修改成功");
        return ResultUtil.ok("修改成功", topic);
    }

    @Override // 继承接口的删除 返回值0(失败),1(成功)
    @Transactional
    public Result deleteTopicById(Long topicId) {
        try {
            logger.info("开始删除topic，topicId===>{}", topicId);
            int delete = topicMapper.deleteById(topicId);
            if (delete == EnableVariable.UNABLE){
                logger.info("删除失败");
                throw new ServiceException(ErrorCodeEnum.SQL_DELETE_ERROR, "删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.info("删除出现异常");
            throw new ServiceException(ErrorCodeEnum.SQL_DELETE_ERROR, "删除出现异常");
        }
        logger.info("删除成功");
        return ResultUtil.ok("删除成功");
    }

    @Override // 继承接口的查询全部
    public List<TopicVo> getAllTopic() {
        List<Topic> topicList = this.topicMapper.getAllTopic();
        List<TopicVo> topicVoList = new ArrayList<>();
        topicList.forEach((item)->{
            TopicVo topicVo = new TopicVo();
            topicVo.setTopicId(item.getTopicId());
            topicVo.setUsername(item.getUsername());
            topicVo.setUserId(item.getUserId());
            topicVo.setCreateTime(DateUtils.format(item.getCreateTime(), DateUtils.DATE_TIME_PATTERN));
            topicVo.setContents(item.getContents());
            topicVo.setGoodId(item.getGoodId());
            topicVo.setGoodName(item.getGoodName());
            topicVo.setNum(item.getNum());
            topicVo.setOrderId(item.getOrderId());
            topicVo.setOrderNum(item.getOrderNum());
            topicVoList.add(topicVo);
        });
        return topicVoList;
    }


    @Override // 继承接口的按主键查询 返回Entity实例
    public TopicVo getTopicById(Long topicId) {
        Topic topic = this.topicMapper.selectById(topicId);
        TopicVo topicVo = new TopicVo();
        topicVo.setTopicId(topic.getTopicId());
        topicVo.setUsername(topic.getUsername());
        topicVo.setUserId(topic.getUserId());
        topicVo.setCreateTime(DateUtils.format(topic.getCreateTime(), DateUtils.DATE_TIME_PATTERN));
        topicVo.setContents(topic.getContents());
        topicVo.setGoodId(topic.getGoodId());
        topicVo.setGoodName(topic.getGoodName());
        topicVo.setNum(topic.getNum());
        topicVo.setOrderId(topic.getOrderId());
        topicVo.setOrderNum(topic.getOrderNum());
        return topicVo;
    }

}
